package com.one.controller;

import com.one.pojo.DogRecord;
import com.one.pojo.OneUser;
import com.one.pojo.Store;
import com.one.service.DogRecordService;
import com.one.service.OneUserService;
import com.one.service.StoreService;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import com.one.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description  哈士奇管理
 * @Author semstouch
 * @Date 2017/4/11
 **/
@Controller
@ControllerAdvice
public class DogController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private OneUserService oneUserService;
    @Resource
    private StoreService storeService;
    @Resource
    private DogRecordService dogRecordService;
    @Resource(name = "ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public DogController() {
        super("callback");
    }

    /**
     * 购买哈士奇
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/operateDog", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult operateDog(HttpServletRequest request, String token, String userId) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        if (dogRecordService.findByDailyAndUserId(StringUtils.getStringMonthShort(), userId) != null) {
            jsonResult.setCode("-1");
            jsonResult.setMsg("本月升级次数已用完！");
            return jsonResult;
        }
        OneUser oneUser = oneUserService.findByUserId(userId);
        Store store = storeService.findByUserId(userId);
        BigDecimal chicksNum = store.getChicksNum();
        if (chicksNum.intValue() < 100) {
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户小鸡数量不够|Your chicken is not enough");
            return jsonResult;
        }
        String dogLevel = oneUser.getDogLevel();
        if (dogLevel.equals("")) {
            oneUser.setDogLevel("1");
            chicksNum = chicksNum.subtract(new BigDecimal(100));
            store.setChicksNum(chicksNum);
            oneUserService.updateOneUserAndStore(oneUser, store);
        } else {
            Integer level = Integer.valueOf(dogLevel);
            if (level == 9) {
                jsonResult.setCode("-1");
                jsonResult.setMsg("等级已经满了，无法升级！");
                return jsonResult;
            }
            level += 1;
            oneUser.setDogLevel(level.toString());

            DogRecord dogRecord = new DogRecord();
            dogRecord.setStatus("1");
            dogRecord.setUserId(userId);
            dogRecord.setDaily(StringUtils.getStringMonthShort());

            oneUserService.updateOneUserAndStore(oneUser, store, dogRecord);
        }

        Map dog = new HashMap();
        dog.put("id", userId);
        dog.put("dogLevel", oneUser.getDogLevel());
        jsonResult.setCode("1");
        jsonResult.setData(dog);
        return jsonResult;
    }

    /**
     * 查询本月免费升级哈士奇机会
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/isHaveFreeUpdateDog", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult isHaveFreeUpdateDog(HttpServletRequest request, String token, String userId) {
        JSONResult jsonResult = new JSONResult();
        if (!cacheProvider.get(userId).equals(token)) {
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        if (dogRecordService.findByDailyAndUserId(StringUtils.getStringMonthShort(), userId) == null) {
            jsonResult.setData(1);
            jsonResult.setCode("1");
        } else {
            jsonResult.setCode("-1");
            jsonResult.setMsg("本月升级次数已用完！");
        }
        return jsonResult;
    }
}
